package com.mexico.inloancash.utils;

import android.content.Context;
import android.content.SharedPreferences;

import androidx.annotation.NonNull;

public class MyPreferencesManager {
    private static SharedPreferences mSharedPreferences;
    private static Context mContext;

    public static void initPreferencesManager(Context context) {
        mContext = context;
        if (mSharedPreferences == null) {
            int mode = Context.MODE_PRIVATE;//不再使用Build.VERSION.SDK_INT >= 11 ? Context.MODE_MULTI_PROCESS : Context.MODE_WORLD_READABLE;
            mSharedPreferences = context.getSharedPreferences("daikuan1", mode);
        }
    }

    public static SharedPreferences newShareprefenceByName(Context context, String name) {
        mContext = context;
        int mode = Context.MODE_PRIVATE;//不再使用Build.VERSION.SDK_INT >= 11 ? Context.MODE_MULTI_PROCESS : Context.MODE_WORLD_READABLE;
        return context.getSharedPreferences(name, mode);
    }

    public static SharedPreferences getMySharedPreferences() {
        if (mSharedPreferences == null) {
            initPreferencesManager(mContext);
        }
        return mSharedPreferences;
    }

    public static SharedPreferences.Editor getMyEditor(SharedPreferences preferences) {
        return preferences.edit();
    }

    public static boolean putLong(String key, long value) {
        SharedPreferences.Editor editor = getMyEditor(getMySharedPreferences());
        editor.putLong(key, value);
        return editor.commit();
    }

    public static Long getLong(String key, long defValue) {
        mSharedPreferences = getMySharedPreferences();
        return mSharedPreferences.getLong(key, defValue);
    }

    @NonNull
    public static String getString(String key, String defValue) {
        mSharedPreferences = getMySharedPreferences();
        return mSharedPreferences.getString(key, defValue);
    }

    public static boolean putString(String key, String value) {
        SharedPreferences.Editor editor = getMyEditor(getMySharedPreferences());
        editor.putString(key, value);
        return editor.commit();
    }

    public static boolean putInt(String key, int value) {
        SharedPreferences.Editor editor = getMyEditor(getMySharedPreferences());
        editor.putInt(key, value);
        return editor.commit();
    }

    public static int getInt(String key, int defValue) {
        mSharedPreferences = getMySharedPreferences();
        return mSharedPreferences.getInt(key, defValue);
    }

    public static boolean putLogin(boolean isLogin) {
        SharedPreferences.Editor editor = getMyEditor(getMySharedPreferences());
        editor.putBoolean("isLogin", isLogin);
        return editor.commit();
    }

    public static boolean getLogin() {
        mSharedPreferences = getMySharedPreferences();
        return mSharedPreferences.getBoolean("isLogin", false);
    }

    public static boolean putNormal(boolean normal) {
        SharedPreferences.Editor editor = getMyEditor(getMySharedPreferences());
        editor.putBoolean("isnormal", normal);
        return editor.commit();
    }

    public static boolean getNormal() {
        mSharedPreferences = getMySharedPreferences();
        return mSharedPreferences.getBoolean("isnormal", false);
    }

    public static boolean putRecovery(boolean normal) {
        SharedPreferences.Editor editor = getMyEditor(getMySharedPreferences());
        editor.putBoolean("recovery", normal);
        return editor.commit();
    }

    public static boolean getRecovery() {
        mSharedPreferences = getMySharedPreferences();
        return mSharedPreferences.getBoolean("recovery", false);
    }

    //第一次进去app 需要弹窗 更新弹窗状态
    public static boolean putIsShow(boolean agreement) {
        SharedPreferences.Editor editor = getMyEditor(getMySharedPreferences());
        editor.putBoolean("agreement", agreement);
        return editor.commit();
    }

    public static boolean getIsShow() {
        mSharedPreferences = getMySharedPreferences();
        return mSharedPreferences.getBoolean("agreement", false);
    }


    //保存谷歌ID--------------------------------------------------
    public static boolean putGoogleID(String googleId) {
        SharedPreferences.Editor editor = getMyEditor(getMySharedPreferences());
        editor.putString("GoogleID_", googleId);
        return editor.commit();
    }

    public static String getGoogleID() {
        mSharedPreferences = getMySharedPreferences();
        return mSharedPreferences.getString("GoogleID_", "");
    }
    //保存谷歌ID--------------------------------------------------


    //保存订单ID--------------------------------------------------
    public static boolean putOrderID(String OrderID) {
        SharedPreferences.Editor editor = getMyEditor(getMySharedPreferences());
        editor.putString("OrderID_", OrderID);
        return editor.commit();
    }

    public static String getOrderID() {
        mSharedPreferences = getMySharedPreferences();
        return mSharedPreferences.getString("OrderID_", "");
    }
    //保存订单ID--------------------------------------------------


    //adjust 渠道号  trackerName--------------------------------------------------
    public static boolean putTrackerName(String OrderID) {
        SharedPreferences.Editor editor = getMyEditor(getMySharedPreferences());
        editor.putString("trackerName", OrderID);
        return editor.commit();
    }

    public static String getTrackerName() {
        mSharedPreferences = getMySharedPreferences();
        return mSharedPreferences.getString("trackerName", "");
    }
    //adjust 渠道号  trackerName--------------------------------------------------



    //第一次进去app
    public static boolean isFirstComing(boolean agreement) {
        SharedPreferences.Editor editor = getMyEditor(getMySharedPreferences());
        editor.putBoolean("isFirstComing", agreement);
        return editor.commit();
    }

    public static boolean getIsFirstComing() {
        mSharedPreferences = getMySharedPreferences();
        return mSharedPreferences.getBoolean("isFirstComing", true);
    }

    //===============================保存 设备唯一id=========start===========================
    public static boolean putDeviceID(String device_id) {
        SharedPreferences.Editor editor = getMyEditor(getMySharedPreferences());
        editor.putString("device_id", device_id);
        return editor.commit();
    }

    public static String getDeviceID() {
        mSharedPreferences = getMySharedPreferences();
        return mSharedPreferences.getString("device_id","");
    }
    //===============================保存 设备唯一id=========end===========================


    //===============================上次活跃时间戳 我的理解就是上次打开app的时间=========start===========================
    public static boolean putLast_login_time(long time) {
        SharedPreferences.Editor editor = getMyEditor(getMySharedPreferences());
        editor.putLong("last_login_time", time);
        return editor.commit();
    }

    public static long getLast_login_time() {
        mSharedPreferences = getMySharedPreferences();
        return mSharedPreferences.getLong("last_login_time",0);
    }
    //===============================上次活跃时间戳 我的理解就是上次打开app的时间=========end===========================



    //-------------------------------应用是否首次安装------------------
    public static boolean putFirstInstall(boolean install) {
        SharedPreferences.Editor editor = getMyEditor(getMySharedPreferences());
        editor.putBoolean("isFirstInstall", install);
        return editor.commit();
    }

    public static boolean getIsFirstInstall() {
        mSharedPreferences = getMySharedPreferences();
        return mSharedPreferences.getBoolean("isFirstInstall", false);
    }
    //-------------------------------应用是否首次安装------------------



    // AppsFlyer 渠道号  trackerName--------------------------------------------------
    public static boolean putAppsFlyerTrackerName(String OrderID) {
        SharedPreferences.Editor editor = getMyEditor(getMySharedPreferences());
        editor.putString("appsFlyer_trackerName", OrderID);
        return editor.commit();
    }

    public static String getAppsFlyerTrackerName() {
        mSharedPreferences = getMySharedPreferences();
        return mSharedPreferences.getString("appsFlyer_trackerName", "");
    }
    // AppsFlyer 渠道号  trackerName--------------------------------------------------
}
